﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Core
{
    using System.IO;
    using System.Reflection;

    /// <summary>
    /// Форма отчёта HTML
    /// </summary>
    public partial class HTMLReport : Form
    {
        private SchoolClass SClass = null;

        public HTMLReport(string Caption, string path)
        {
            InitializeComponent();
            this.Text += ": " + Caption;
            webBrowser1.Url = new Uri(path);
            SaveDataButton.Enabled = false;
            PlugItems.DropDownItems.Clear();
        }

        public HTMLReport(SchoolClass SchClass)
        {
            InitializeComponent();
            SClass = SchClass;
            this.Text += ": " + SchClass.ClassTitle;
            RetingManagement.NewVisualReport(ref webBrowser1, SchClass);
            LoadPlugIn();
        }

        public HTMLReport()
        {
            InitializeComponent();
            SaveReportButton.Enabled = false;
            SaveDataButton.Enabled = false;
            this.Text = "Новый отчёт";
            PlugItems.DropDownItems.Clear();
        }

        public void LoadPlugIn()
        {
            try
            {
                PlugItems.DropDownItems.Clear();
                for (int i = 0; i < AppOptions.modules.Length; i++)
                {
                    PlugItem item = new PlugItem();
                    Assembly myAsm = Assembly.LoadFile(AppOptions.modules[i].library);
                    object value = myAsm.CreateInstance(AppOptions.modules[i].classname);
                    item.plugin = (value as _BasePlugin);
                    if (item.plugin != null)
                    {
                        item.owner = this.ParentForm;
                        item.getclass = new GetData(GetClass);
                        item.Text = item.plugin.PlugName;
                        PlugItems.DropDownItems.Add(item);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Ошибка загрузки модулей PlugIn.","Ошибка",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        public SchoolClass GetClass()
        {
            return SClass;
        }

        private void SaveReportButton_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }
        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            RetingManagement.SaveData(saveFileDialog1.FileName, SClass);
        }
        private void SaveDataButton_Click(object sender, EventArgs e)
        {
            saveFileDialog2.ShowDialog();
        }
        private void сформироватьОтчётToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                SClass = RetingManagement.ReadData(openFileDialog1.FileName);
                RetingManagement.NewVisualReport(ref webBrowser1, SClass);
                SaveReportButton.Enabled = true;
                SaveDataButton.Enabled = true;
                this.Text = "Отчёт : " + SClass.ClassTitle;
                LoadPlugIn();
            }
            catch (Exception ez)
            {
                MessageBox.Show(ez.Message, "Ошибка формирования отчёта");
            }
        }
        private void saveFileDialog2_FileOk(object sender, CancelEventArgs e)
        {
            string txt = webBrowser1.DocumentText;
            StreamWriter writer = new StreamWriter(saveFileDialog2.FileName);
            writer.Write(txt);
            writer.Close();
        }
        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void HTMLReport_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (SClass != null)
            {
                DialogResult result = MessageBox.Show("Сохранить данные:\n" + SClass.ClassTitle,
                    "Менеджер рейтинга", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    saveFileDialog2.ShowDialog();
                }
                if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
                if (result == DialogResult.No)
                {
                    webBrowser1.Dispose();
                }
            }
        }
    }
}